package cn.edu.ldk.controller;

import cn.edu.ldk.common.CommonJsonResult;
import cn.edu.ldk.pojo.Address;
import cn.edu.ldk.service.AddressService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController  //@RestController=@Controller + @ResponseBody  加了这个注解，无论下面的方法返回什么类型，无论是数组字符串，都会返回json串
public class AddressController {
@Resource
private AddressService addressService;
    //localhost:8081/provider/getAddressInfo 返回JSON数据给浏览器
    @GetMapping("/provider/getAddressInfo")
    public List<Address> getAddressInfo(){
        List<Address> list = addressService.findAllAddress();
//        for (Address address : list) {
//            System.out.println(address);
//        }
        return list;
    }

    //添加
    //将 Json 对象转成实体对象@RequestBody
    @PostMapping("/provider/save")
    public CommonJsonResult<Void> save( @RequestBody Address address){
        CommonJsonResult<Void> result = addressService.save(address);
        return result;
    }

    //根据Id 查询具体的某条地址信息
    //localhost:8081/getAddressinfoByid/1  //@PathVariable 使用RestFull风格
    @GetMapping("/provider/getAddressinfoByid/{id}")
    public CommonJsonResult<Address> getAddressinfoByid(@PathVariable("id") Integer id){
        System.out.println("调用8081的getAddressinfoByid方法");
        return addressService.getAddressinfoByid(id);
    }
}
